package com.silence.springboot.listeners;

import com.mybatisflex.annotation.InsertListener;
import com.silence.springboot.base.BaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author silence
 * @email 792245022@qq.com
 * @create 2023-09-20 9:40
 */
@Slf4j
@Component
public class MybatisFlexInsertListener implements InsertListener {

    /**
     * 新增监听器
     *
     * @param entity 实体
     */
    @Override
    public void onInsert(Object entity) {
        if (entity instanceof BaseEntity baseEntity) {
            baseEntity.setCreateTime(LocalDateTime.now());
            baseEntity.setUpdateTime(LocalDateTime.now());
            baseEntity.setCreateUser("silence");
            baseEntity.setUpdateUser("silence");
        } else {
            log.warn("该实体没有继承基础类字段");
        }
    }
}
